<?php

session_start();
if (!empty($_SESSION['username'])) {
$conn_str = 'host=dbsrv1.cdf.toronto.edu dbname=csc309g23 user=csc309g23 password=ipheid5h';
$conn = pg_connect($conn_str);

$query = 'SELECT postid FROM wall order by postid ASC';
$current_postid = pg_query($conn, $query);
$postid = pg_fetch_row($current_postid);
$temp = 1;
while ($temp == $postid[0])  {
	$postid = pg_fetch_row($current_postid);
	$temp = $temp + 1; 
	}	
$next = $temp;

if ($_POST['message'] != "")
    pg_query('INSERT into wall values (' . $next . ', \'' . $_SESSION['username'] . '\', \'' . $_POST['postfriend'] . '\', current_timestamp, \'' . $_POST['message'] . '\')');
header('Location: ' . $_SERVER['HTTP_REFERER']);
} else {
header('Location: /');
}
?>
